﻿using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;

namespace CheckNoteWpf.API
{
    /// <summary>
    /// 配置Key
    /// </summary>
    public enum ConfigKey
    {
        /// <summary>
        /// 主窗口背景色 #FFFFB6C2 RGB(255,182,193)
        /// </summary>
        BgColorHex,

        /// <summary>
        /// 通用按钮颜色 #FF4BAADC
        /// </summary>
        CommonBtnColorHex,

        /// <summary>
        /// 通用按钮字体颜色 #FFFFF8F8
        /// </summary>
        CommonBtnTextColorHex,

        /// <summary>
        /// 内容文本颜色 #FF15445D
        /// </summary>
        ContentColorHex,
    }

    public static class GlobalDefine
    {
        /// <summary>
        /// 获取配置值
        /// </summary>
        public static string GetConfigVal(ConfigKey key)
        {
            Init();

            if (configDict != null && configDict.TryGetValue(key, out ConfigModel model))
            {
                return model.Value;
            }
            if (defaultConfigDict != null && defaultConfigDict.TryGetValue(key, out model))
            {
                return model.Value;
            }
            MessageBox.Show("无配置，Key：" + key.ToString());
            return "";
        }

        /// <summary>
        /// 选项列表保存数据路径
        /// </summary>
        public const string InfoListItemDataPath = "data.json";

        /// <summary>
        /// 配置文件路径
        /// </summary>
        public const string ConfigDataPath = "config.json";

        private static Dictionary<ConfigKey, ConfigModel> configDict;

        private static bool inited = false;

        public static void Init()
        {
            if (inited) return;
            inited = true;

            if (!File.Exists(ConfigDataPath)) CreateDefaultConfigFile();

            FileStream fs = File.Open(ConfigDataPath, FileMode.OpenOrCreate);
            StringBuilder sb = new StringBuilder();
            using (StreamReader sr = new StreamReader(fs))
            {
                while (sr.Peek() != -1)
                {
                    sb.Append(sr.ReadLine());
                }
            }
            string data = sb.ToString();
            configDict = JsonConvert.DeserializeObject<Dictionary<ConfigKey, ConfigModel>>(data);
        }

        private static void CreateDefaultConfigFile()
        {
            FileStream fs = File.Open(ConfigDataPath, FileMode.Create);
            using (StreamWriter sw = new StreamWriter(fs))
            {
                sw.WriteLine(JsonConvert.SerializeObject(defaultConfigDict));
            }
        }

        /// <summary>
        /// 默认配置
        /// </summary>
        private static Dictionary<ConfigKey, ConfigModel> defaultConfigDict = new Dictionary<ConfigKey, ConfigModel>()
        {
            [ConfigKey.BgColorHex] = new ConfigModel(ConfigKey.BgColorHex, "BgColorHex", "#FFFFB6C2", "主面板底色"),
            [ConfigKey.CommonBtnColorHex] = new ConfigModel(ConfigKey.CommonBtnColorHex, "CommonBtnColorHex", "#FF4BAADC", "通用按钮颜色"),
            [ConfigKey.CommonBtnTextColorHex] = new ConfigModel(ConfigKey.CommonBtnTextColorHex, "CommonBtnTextColorHex", "#FFFFF8F8", "通用按钮字体颜色"),
            [ConfigKey.ContentColorHex] = new ConfigModel(ConfigKey.ContentColorHex, "ContentColorHex", "#FF15445D", "内容文本颜色"),
        };
    }

    /// <summary>
    /// 配置模型
    /// </summary>
    public class ConfigModel
    {
        public ConfigKey CfgType { get; set; }
        public string Key { get; set; }
        public string Value { get; set; }
        public string Desc { get; set; }

        public ConfigModel(ConfigKey cfgType, string key, string value, string desc = "")
        {
            CfgType = cfgType;
            Key = key;
            Value = value;
            Desc = desc;
        }
    }
}
